package com.catering.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.catering.dao.CateringOrderLogDao;
import com.catering.entity.CateringOrderLog;
import com.catering.service.CateringOrderLogService;
import com.catering.until.CommonUtils;
import com.catering.until.ResultData;
import com.catering.until.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class CateringOrderLogServiceImpl extends ServiceImpl<CateringOrderLogDao, CateringOrderLog> implements CateringOrderLogService {
	
	@Autowired
	private CateringOrderLogDao cateringOrderLogDao;
	
	@Override
	public ResultData select(Map<String,String> map) {
		try {
			QueryWrapper wrapper = CommonUtils.getKeySet(map,"");
			List<CateringOrderLog> list = new ArrayList<CateringOrderLog>();
			if(CommonUtils.checkNoNull(map.get("page"))) {
				IPage<CateringOrderLog> page = new Page<>(Integer.valueOf(map.get("page")),Integer.valueOf(map.get("limit")));
				IPage<CateringOrderLog> pageList = cateringOrderLogDao.selectPage(page, wrapper);
				Integer count = cateringOrderLogDao.selectCount(wrapper);
				list = pageList.getRecords();
				return ResultData.success(list,count);
			}else {
				list = cateringOrderLogDao.selectList(wrapper);
				return ResultData.success(list);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		return ResultData.error(ResultEnum.SERVER_ERROR.getCode(),ResultEnum.SERVER_ERROR.getMsg());
	}

	@Override
	public ResultData insert(Map<String,String> map) {
		try {
			CateringOrderLog vo = JSON.parseObject(JSON.toJSONString(map), CateringOrderLog.class);
			List<String> oders = Arrays.asList(vo.getOrderStatus().split(","));
			if(oders.size() > 1){
				for(String o : oders){
					vo.setCreateTime(CommonUtils.getTime(23));
					vo.setOrderStatus(o);
					cateringOrderLogDao.insert(vo);
				}
			}else {
				vo.setCreateTime(CommonUtils.getTime(23));
				cateringOrderLogDao.insert(vo);
			}
			return ResultData.success();
		}catch (Exception e) {
			e.printStackTrace();
			return ResultData.error(ResultEnum.INSERT_ERROR.getCode(),ResultEnum.INSERT_ERROR.getMsg());
		}
	}

	@Override
	public ResultData update(Map<String,String> map) {
		try {
			CateringOrderLog vo = JSON.parseObject(JSON.toJSONString(map), CateringOrderLog.class);
			cateringOrderLogDao.updateById(vo);
			return ResultData.success();
		}catch (Exception e) {
			e.printStackTrace();
			return ResultData.error(ResultEnum.UPDATE_ERROR.getCode(),ResultEnum.UPDATE_ERROR.getMsg());
		}
	}

	@Override
	public ResultData delete(Map<String,String> map) {
		try {
			CateringOrderLog vo = JSON.parseObject(JSON.toJSONString(map), CateringOrderLog.class);
			List<String> list = Stream.of(vo.getSysId().split(",")).collect(Collectors.toList());
			cateringOrderLogDao.deleteBatchIds(list);
			return ResultData.success();
		}catch (Exception e) {
			e.printStackTrace();
		}
		return ResultData.error(ResultEnum.DELETE_ERROR.getCode(),ResultEnum.DELETE_ERROR.getMsg());
	}
}
